package cn.com.oceansoft.business.szjf.controller;

import cn.com.oceansoft.business.base.controller.BaseController;
import cn.com.oceansoft.business.base.entity.Pager;
import cn.com.oceansoft.business.base.entity.Query;
import cn.com.oceansoft.business.base.entity.Result;
import cn.com.oceansoft.business.sys.entity.SysUser;
import cn.com.oceansoft.business.szjf.entity.Position;
import cn.com.oceansoft.business.szjf.entity.Pssh;
import cn.com.oceansoft.business.szjf.entity.Regional;
import cn.com.oceansoft.business.szjf.service.IPsshService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.*;

/**
 * Created by Administrator on 2017/6/26.
 * 评审资料审核控制器
 */
@Controller
@RequestMapping("pssh")
public class PsshController extends BaseController {
    @Resource
    private IPsshService psshService;

    /**
     * 进入评审资料列表页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "index")
    public String gotoPssh(HttpServletRequest request, Model model) {
        List<Regional> regional = psshService.getRegional();
        model.addAttribute("regional",regional);
        SysUser s = (SysUser) request.getSession().getAttribute("session_user");
        //判断查看的人是否是管理员
        if ("0".equals(s.getPositionlevel())) {
            //管理员
            return "szjf/pssh/pssh";
        } else {
            //分局管理员
            return "szjf/pssh/pssh_fj";
        }


    }

    /**
     *获取评审初审数据
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getPscs")
    public Result getYsch(Query query,HttpServletRequest request){
        Map<String, String> map = new HashMap<>();
        map.put("projectno", request.getParameter("projectno"));//查询的project编号
        map.put("regionid", request.getParameter("regionid"));//查询的project区域
        try {
            if (request.getParameter("projectname") == null || request.getParameter("projectname") == "" || "undefined".equals(request.getParameter("projectname"))) {
                //如果projectname没有数据的时候忽略这个条件
            } else {
                map.put("projectname", URLDecoder.decode(request.getParameter("projectname"), "UTF-8"));//查询的project名字
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("projectstatus", "0");//根据评审初审0，评审受理1，抽取专家2 确定状态
        query.setMap(map);
        Pager<Pssh> pager = psshService.showFirstReview(query);
        return new Result(pager);
    }

    /**
     * 显示分发选项页面
     *
     * @param model
     * @param guid
     * @return
     */
    @RequestMapping(value = "distribute/{guid}")
    public String distribute(Model model, @PathVariable("guid") String guid) {
        String type = "1";
        List<Position> list = psshService.getDistributePosition(type);
        model.addAttribute("ProjectGuid", guid);
        model.addAttribute("position", list);
        return "szjf/pssh/ffdw";
    }

    /**
     * 分发确定
     * @param request
     * @param guid
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "giveOut/{ProjectGuid}")
    public Result giveOut(HttpServletRequest request,@PathVariable("ProjectGuid") String guid) {
        SysUser s = (SysUser) request.getSession().getAttribute("session_user");
        String distributerole = request.getParameter("distributeId");
        String distributeName = request.getParameter("distributeName");
        try {
            psshService.giveOut(guid,distributerole,s.getGuid(),distributeName);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(Result.FAILURE,"操作失败");
        }
        return  new Result(Result.SUCC,"操作成功");
    }

    /**
     *获取评审受理数据
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getPssl")
    public Result getPssl(Query query,HttpServletRequest request){
        Map<String, String> map = new HashMap<>();
        SysUser s = (SysUser) request.getSession().getAttribute("session_user");
        if("0".equals(s.getPositionlevel())){

        }else{
            map.put("distributerole", s.getPositionguid()); //position的guid 显示分局被分配的
        }
        map.put("projectno", request.getParameter("projectno"));//查询的project编号
        map.put("regionid", request.getParameter("regionid"));//查询的project区域
        try {
            if (request.getParameter("projectname") == null || request.getParameter("projectname") == "" || "undefined".equals(request.getParameter("projectname"))) {
                //如果projectname没有数据的时候忽略这个条件
            } else {
                map.put("projectname", URLDecoder.decode(request.getParameter("projectname"), "UTF-8"));//查询的project名字
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("projectstatus", "1");//根据评审初审0，评审受理1，抽取专家2 确定状态
        query.setMap(map);
        Pager<Pssh> pager = psshService.showFirstReview(query);
        return new Result(pager);
    }

    /**
     * 通过评审受理
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "psslApproval")
    public Result pass(HttpServletRequest request) {
        SysUser s = (SysUser) request.getSession().getAttribute("session_user");
        try {
            psshService.pass(request.getParameter("guid"),s.getGuid());
        }catch (Exception e){
            e.printStackTrace();
            return new Result(Result.FAILURE,"操作失败");
        }
        return  new Result(Result.SUCC,"操作成功");
    }

    /**
     *获取抽取专家数据
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getCqzj")
    public Result getCqzj(Query query,HttpServletRequest request){
        Map<String, String> map = new HashMap<>();
        SysUser s = (SysUser) request.getSession().getAttribute("session_user");
        if("0".equals(s.getPositionlevel())){

        }else{
            map.put("distributerole", s.getPositionguid()); //position的guid 显示分局被分配的
        }
        map.put("projectno", request.getParameter("projectno"));//查询的project编号
        map.put("regionid", request.getParameter("regionid"));//查询的project区域
        try {
            if (request.getParameter("projectname") == null || request.getParameter("projectname") == "" || "undefined".equals(request.getParameter("projectname"))) {
                //如果projectname没有数据的时候忽略这个条件
            } else {
                map.put("projectname", URLDecoder.decode(request.getParameter("projectname"), "UTF-8"));//查询的project名字
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("projectstatus", "2");//根据评审初审0，评审受理1，抽取专家2 确定状态
        query.setMap(map);
        Pager<Pssh> pager = psshService.showFirstReview(query);
        return new Result(Result.SUCC,pager);
    }

    /**
     * 根据project的id获取详细信息
     *
     * @param model
     * @param guid
     * @return
     */
    @RequestMapping("getDetail/{guid}")
    public String getDetail(Model model, @PathVariable("guid") String guid, HttpServletRequest request) {
        Pssh item = psshService.getDetailByPrjNo(guid);
        model.addAttribute("detail", item);
        return "szjf/pssh/detail";
    }

    /**
     * 显示抽取专家页面
     *
     * @param model
     * @param guid
     * @return
     */
    @RequestMapping(value = "getZJ/{guid}")
    public String getZJ(Model model, @PathVariable("guid") String guid) {
        model.addAttribute("ProjectGuid", guid);
        return "szjf/pssh/cqzj";
    }

}
